<?php

/**
 * Created by PhpStorm.
 * User: wu
 * Date: 2017/6/16
 * Time: 9:43
 */
namespace V1;
class ArticleContent extends \ResourceClass
{
    public $isActive = true;
    public $remark = '文章内容接口';

    public function post(array $req, array $post, &$result)
    {
        if (!isset($req['uid'])) {
            return 'require UID';
        }
        if (!array_key_exists('payload', $post)) {
            return 'require payload';
        }
        $data = base64_decode($post['payload']);
        @parse_str($data, $data);
        if (!isset($data['article_id'])) {
            return '文章id不能为空';
        }
        if (!isset($data['type'])) {
            return '文章类型不能为空';
        }

        $allowed_type_array = array('text','image','product','video','audio');
        if (!in_array($data['type'],$allowed_type_array)) {
            return '不被允许的类型';
        }
        switch ($data['type']) {
            case 'text':
                $type = \Orm\ArticleContent\ArticleContentClass::TYPE_TEXT;
                break;
            case 'img':
                $type = \Orm\ArticleContent\ArticleContentClass::TYPE_IMG;
                break;
            case 'product':
                $type = \Orm\ArticleContent\ArticleContentClass::TYPE_PRODUCT;
                break;
            case 'video':
                $type = \Orm\ArticleContent\ArticleContentClass::TYPE_VIDEO;
                break;
            case 'audio':
                $type = \Orm\ArticleContent\ArticleContentClass::TYPE_AUDIO;
                break;
        }
        try {
            $article_content_class = new \Orm\ArticleContent\ArticleContentClass();
            $article_content_class['article_id'] = $data['article_id'];
            $article_content_class['type'] = $type;
            $article_content_class['content'] = $data['content'];
            $article_content_class['sort'] = $data['sort'];
            $article_content_class->insert();
            $article_content_class->loadById($article_content_class['id']);
            $result = array(
                'id' => $article_content_class['id'],
                'article_id' => $article_content_class['article_id'],
                'type' => $type,
                'content' => $article_content_class['content'],
                'sort' => $article_content_class['sort'],
                );
        }catch (\Exception $e ) {
            return $e->getMessage();
        }
        return '';
    }

    public function get(array $req, array $post, &$result)
    {

    }

    public function put(array $req, array $post, &$result)
    {
        if (!isset($req['id'])) {
            return 'require ID';
        }
        if (!isset($req['uid'])) {
            return 'require UID';
        }
        if (!array_key_exists('payload', $post)) {
            return 'require payload';
        }
        $data = base64_decode($post['payload']);
        @parse_str($data, $data);
        if (!isset($data['article_id'])) {
            return '文章id不能为空';
        }
        if (!isset($data['type'])) {
            return '文章类型不能为空';
        }

        $allowed_type_array = array('text','img','product','video','audio');
        if (!in_array($data['type'],$allowed_type_array)) {
            return '不被允许的类型';
        }
        switch ($data['type']) {
            case 'text':
                $type = \Orm\ArticleContent\ArticleContentClass::TYPE_TEXT;
                break;
            case 'img':
                $type = \Orm\ArticleContent\ArticleContentClass::TYPE_IMG;
                break;
            case 'product':
                $type = \Orm\ArticleContent\ArticleContentClass::TYPE_PRODUCT;
                break;
            case 'video':
                $type = \Orm\ArticleContent\ArticleContentClass::TYPE_VIDEO;
                break;
            case 'audio':
                $type = \Orm\ArticleContent\ArticleContentClass::TYPE_AUDIO;
                break;
        }
        try {
            $article_content_class = new \Orm\ArticleContent\ArticleContentClass();
            $article_content_class->loadById($req['id']);
            $article_content_class['article_id'] = $data['article_id'];
            $article_content_class['type'] = $type;
            $article_content_class['content'] = $data['content'];
            $article_content_class['sort'] = $data['sort'];
            $article_content_class->update();
            $article_content_class->loadById($article_content_class['id']);
            $result = array(
                'id' => $article_content_class['id'],
                'article_id' => $article_content_class['article_id'],
                'type' => $type,
                'content' => $article_content_class['content'],
                'sort' => $article_content_class['sort'],
            );
        }catch (\Exception $e ) {
            return $e->getMessage();
        }
        return '';


    }
    public function delete(array $req, array $post, &$result)
    {
        if (!isset($req['id'])) {
            return 'require ID';
        }
        if (!isset($req['uid'])) {
            return 'require UID';
        }

        try {
            $article_content_class = new \Orm\ArticleContent\ArticleContentClass();
            $article_content_class->loadById($req['id']);
            $article_class = new \Orm\Article\ArticleClass();
            $article_class->loadById($article_content_class['article_id']);
            if ($req['uid'] != $article_class['uid']) {
                return '对不起，这个文章作者不是你'.$article_class['uid'].'，不可删除';
            }

            $article_content_class->delete($req['id']);
        }catch (\Exception $e ) {
            return $e->getMessage();
        }
        return '';
    }
}